export interface bluetootInfo {
	deviceId : string
	name : string
	RSSI : string
}

//初始化蓝牙
export const openBluetoothAdapte = () => {
	console.log("openBluetoothAdapte")
	return new Promise((resolve, reject) => {
		uni.openBluetoothAdapter({
			success: () => {
				resolve({})
			},
			fail: () => {
				reject({
					msg: "请打开蓝牙"
				})
			}
		})
	})
}

//开始搜索蓝牙设备 
export const startBluetoothDevicesDiscovery = () => {
	return new Promise((resolve, reject) => {
		uni.startBluetoothDevicesDiscovery({
			success: (req : any) => {
				setTimeout(() => {
					resolve(req)
				}, 1000)
			},
			fail: (error) => {
				reject(error)
			}
		})
	})
}

//创建链接
export const createBLEConnection = (deviceId : string) => {
	return new Promise((resolve, reject) => {
		uni.createBLEConnection({
			deviceId: deviceId,
			success: (res : any) => {
				setTimeout(() => {
					resolve(res)
				}, 1000)
			}, fail: (error) => {
				reject(error)
			}
		})
	})
}

export const getBluetoothDevices = () : Promise<{ list : Array<any> }> => {
	const list : Array<any> = []
	return new Promise((resolve, reject) => { 
			uni.getBluetoothDevices({
				success: (res : any) => {
					for (var i = 0; i < res.devices.length; ++i) {
						if (res.devices[i].name != '') {
							list.push({
								deviceId: res.devices[i].deviceId,
								name: res.devices[i].name,
								RSSI: res.devices[i].RSSI
							})
						}
			
					}
					uni.stopBluetoothDevicesDiscovery()
					resolve({ list })
				},
				fail: (err : any) => {
					reject(err)
				}
			}) 
	})
}

export const getBLEDeviceServices = (deviceId : string) : Promise<{ services : Array<string> }> => {
	return new Promise((resolve, reject) => {
		uni.getBLEDeviceServices({
			deviceId: deviceId,
			success: (res1 : any) => {
				resolve({ services: res1.services.map((item : any) => item.uuid) })
			}, fail(error : any) {
				reject(error)
			}
		})
	})
}


export const getBLEDeviceCharacteristics = (deviceId : string, serviceList : Array<String>, state : any) => {
	return new Promise((resolve, reject) => {
		let num = 0
		uni.getBLEDeviceCharacteristics({
			deviceId: deviceId,
			serviceId: serviceList[state.value.num] as string,
			success: async (res : any) => { 
				for (var i = 0; i < res.characteristics.length; ++i) {
					var properties = res.characteristics[i].properties
					var item = res.characteristics[i].uuid
					if (!state.value.write) {
						if (properties.write) {
							state.value.writeCharaterId = item
							state.value.writeServiceId = serviceList[state.value.num]
							state.value.write = true
						}
					}

					if (!state.value.notify) {
						if (properties.notify) {
							state.value.notifyCharaterId = item
							state.value.notifyServiceId = serviceList[state.value.num]
							state.value.notify = true
						}
					}

					if (!state.value.read) {
						if (properties.read) {
							state.value.readCharaterId = item
							state.value.readServiceId = serviceList[state.value.num]
							state.value.read = true
						}
					}
				}
				resolve({})
			}
		})
	})
}